/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package dao;

import dto.MessageDTO;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * Message DAO
 * @author Hung Cuong <nhc.hcmuns at gmail.com>
 */
public class MessageDAO {
    MSAccessProvider _Provider;

    public MessageDAO() {
        _Provider = new MSAccessProvider();
    }

    /**
     * Insert message
     * @param dto
     */
    public void insert(MessageDTO msg) {
        
        String insertQuery = "INSERT INTO Message(MsgFrom, MsgTo, MsgSubject, MsgBody, MsgDate, MsgType) VALUES ('" +
                                                msg.getFrom() + "', '" +
                                                msg.getTo() + "', '" +
                                                msg.getSubject() + "', '" +
                                                msg.getBody() + "', '" +
                                                msg.getSentDate() + "', '" +
                                                msg.getType() + "')" ;

        _Provider.excuteNonQuery(insertQuery);
    }

    /*
    public void updateMessage(MessageDTO contact) {
        String updateQuery = "UPDATE ...";

        _Provider.excuteNonQuery(updateQuery);
    }
    */

    /**
     * Delete message
     * @param dto
     */
    public void delete(MessageDTO msg) {
        
        String deleteQuery = "DELETE FROM Message WHERE Id=" + msg.getId();

        _Provider.excuteNonQuery(deleteQuery);
    }

    public ArrayList<MessageDTO> getMessageFromType(String type)
    {
        ArrayList<MessageDTO> list = new ArrayList<MessageDTO>();
        String query = "SELECT * FROM Message Where MsgType='" + type + "'";
        ResultSet rs = _Provider.executeQuery(query);

        if(rs != null){
            try {
                while (rs.next()) {
                    MessageDTO msg = new MessageDTO();
                    msg.setId(rs.getInt(1));
                    msg.setFrom(rs.getString(2));
                    msg.setTo(rs.getString(3));
                    msg.setSubject(rs.getString(4));
                    msg.setBody(rs.getString(5));
                    msg.setSentDate(rs.getString(6));
                    msg.setType(rs.getString(7));
                    list.add(msg);
                }
            } catch (SQLException ex) {
                Logger.getLogger(MessageDAO.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

        return list;
    }
    /**
     * Get all messages
     * @return
     */
    public ArrayList<MessageDTO> getAllRecords(){
        ArrayList<MessageDTO> list = new ArrayList<MessageDTO>();

        String selectAllQuery = "SELECT * FROM Message";

        ResultSet rs = _Provider.executeQuery(selectAllQuery);

        if(rs != null){
            try {
                while (rs.next()) {
                    MessageDTO msg = new MessageDTO();
                    msg.setId(rs.getInt(1));
                    msg.setFrom(rs.getString(2));
                    msg.setTo(rs.getString(3));
                    msg.setSubject(rs.getString(4));
                    msg.setBody(rs.getString(5));
                    msg.setSentDate(rs.getString(6));
                    msg.setType(rs.getString(7));
                    list.add(msg);
                }
            } catch (SQLException ex) {
                Logger.getLogger(MessageDAO.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

        return list;
    }

    /**
     * Get message information
     * @param msgId
     * @return
     */
    public MessageDTO getInfo(int msgId){
        MessageDTO msg = new MessageDTO();

        String contactQuery = "SELECT * FROM Message WHERE Id = " + msgId;

        ResultSet rs = _Provider.executeQuery(contactQuery);

        if(rs != null){
            try {
                rs.next();
                msg.setId(rs.getInt(1));
                msg.setFrom(rs.getString(2));
                msg.setTo(rs.getString(3));
                msg.setSubject(rs.getString(4));
                msg.setBody(rs.getString(5));
                msg.setSentDate(rs.getString(6));
                msg.setType(rs.getString(7));
            } catch (SQLException ex) {
                Logger.getLogger(MessageDAO.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return msg;
    }

    public void changeType(int msgId, String newType){
        String query = "UPDATE Message SET MsgType='" + newType +"' WHERE Id=" + msgId;
        _Provider.executeQuery(query);
    }
}
